package app.android.alampang.tool;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import java.io.File;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 * Created by Supanut on 1/18/2015.
 */
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import java.io.File;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 * Created by Supanut on 1/18/2015.
 */
public class XMLReader
{
    private DocumentBuilderFactory factory;
    private DocumentBuilder builder;
    private Document document;

    private Element element;
    private NodeList nodeList;

    public XMLReader() throws Exception
    {
        this.factory = DocumentBuilderFactory.newInstance();
        this.builder = factory.newDocumentBuilder();
    }


    public void read(String xml) throws Exception
    {
        InputSource source = new InputSource();
        source.setCharacterStream(new StringReader(xml));
        this.document = builder.parse(source);
    }

    public void read(URL url) throws Exception
    {
        InputStream xml = url.openStream();
        this.document = builder.parse(xml);
    }

    public void readFile(File file) throws Exception
    {
//		BufferedReader reader = new BufferedReader(new FileReader(file));
//		String xml = "";
//		while (reader.ready())
//			xml += reader.readLine();
//		reader.close();

        this.document = builder.parse(file);

    }

    public Element moveTo(String key)
    {

        this.nodeList = this.document.getElementsByTagName(key);
        this.element = (Element)this.nodeList.item(0);

        return this.element;
    }

    public Element moveTo(String key, Element element)
    {
        this.nodeList = this.element.getElementsByTagName(key);
        this.element = (Element)this.nodeList.item(0);

        return this.element;
    }

    public int getNodeLength()
    {
        return this.nodeList.getLength();
    }

    public void setNodeIndex(int i)
    {
        this.element = (Element)this.nodeList.item(i);
    }

    public String getContent(String key)
    {
        NodeList nltmp = element.getElementsByTagName(key);
        String ret = "";
        StreamResult output = new StreamResult(new StringWriter());
        try
        {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(new DOMSource(nltmp.item(0)), output);

            ret = output.getWriter().toString();
            //System.out.println(">>>> "+ret);
            ret = ret.substring(key.length()+2, ret.length() - (key.length()+3));
        }
        catch (Exception e)
        {
            return "";
        }

        return ret;
    }

    public String getAttribute(String key)
    {
        return element.getAttributes().getNamedItem(key).getNodeValue();
    }
}
